package cn.xumaoyang.pet.service.impl;


import cn.xumaoyang.basic.service.impl.BaseServiceImpl;
import cn.xumaoyang.basic.util.*;
import cn.xumaoyang.order.domain.PetAcquisitionOrder;
import cn.xumaoyang.org.domain.Employee;
import cn.xumaoyang.org.domain.Shop;
import cn.xumaoyang.org.mapper.EmployeeMapper;
import cn.xumaoyang.org.mapper.ShopMapper;
import cn.xumaoyang.pet.domain.Pet;
import cn.xumaoyang.pet.domain.PetDetail;
import cn.xumaoyang.pet.domain.SearchMasterMsg;
import cn.xumaoyang.pet.mapper.PetDetailMapper;
import cn.xumaoyang.pet.mapper.PetMapper;
import cn.xumaoyang.pet.mapper.SearchMasterMsgMapper;
import cn.xumaoyang.pet.query.SearchMasterMsgQuery;
import cn.xumaoyang.pet.service.ISearchMasterMsgService;
import cn.xumaoyang.user.domain.Logininfo;
import cn.xumaoyang.user.domain.User;
import cn.xumaoyang.user.mapper.UserMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class SearchMasterMsgServiceImpl extends BaseServiceImpl<SearchMasterMsg> implements ISearchMasterMsgService {


    @Autowired
    private UserMapper userMapper;

    @Autowired
    private SearchMasterMsgMapper searchMasterMsgMapper;
    
    @Autowired
    private EmployeeMapper employeeMapper;

    @Autowired
    private ShopMapper shopMapper;

    @Autowired
    private PetMapper petMapper;

    @Autowired
    private PetDetailMapper petDetailMapper;

    @Override
    public List<SearchMasterMsg> getAll() {
        return null;
    }


    //未处理宠物信息的分页查询。
    @Override
    public PageList<SearchMasterMsg> pending(SearchMasterMsgQuery query, Logininfo currentLoginInfo) {
        //设置状态，0为未处理。
        query.setState(0);
        //获取当前登录人。
        Employee employee = employeeMapper.loadByLoginInfoId(currentLoginInfo.getId());
        //判断是否在shop表中存在,根据店铺获取到宠物信息。
        if (employee.getShop_id() != null){
            query.setShopId(employee.getShop_id());
        }
        return super.queryPage(query);
    }


    //已处理的宠物信息的分页查询。
    @Override
    public PageList<SearchMasterMsg> processed(SearchMasterMsgQuery query, Logininfo currentLoginInfo) {
        //设置状态，1为已处理。
        query.setState(1);
        //获取当前登录人。
        Employee employee = employeeMapper.loadByLoginInfoId(currentLoginInfo.getId());
        //判断是否在shop表中存在,根据店铺获取到宠物信息。
        if (employee.getShop_id() != null){
            query.setShopId(employee.getShop_id());
        }
        return super.queryPage(query);
    }


    /**
     * 宠物发布
     * @param searchMasterMsg
     */
    @Override
    public void publish(SearchMasterMsg searchMasterMsg,Long id) {
//        private Long user_id;   //
//        private User user;
//        private Long shop_id;
//        private Shop shop;
        //第一步，获取当前登录人
        //获取到user表里的id.
        User user = userMapper.loadByLoginfoId(id);
        //设置给searchMasterMsgMapper表里的user_id。
        searchMasterMsg.setUser_id(user.getId());


        //第二步，获取最近的门店。
        //获取地址。
        Point point = DistanceUtil.getPoint(searchMasterMsg.getAddress());
        Shop nearestShop = DistanceUtil.getNearestShop(point, shopMapper.loadAll());
        searchMasterMsg.setShop_id(nearestShop.getId());


        //第三步，保存寻主消息。
        searchMasterMsgMapper.add(searchMasterMsg);
    }

    //查询个人。
    @Override
    public PageList<SearchMasterMsg> queryUserMsg(SearchMasterMsgQuery query, Logininfo currentLoginInfo) {
        //获取当前登录角色id。
        User user = userMapper.loadByLoginfoId(currentLoginInfo.getId());
        //设置给给query
        query.setUserId(user.getId());
        return super.queryPage(query);
    }

    //处理寻主消息。
    @Override
    public void handle(Pet pet, Logininfo currentLoginInfo) {
        //修改消息状态。
        searchMasterMsgMapper.updateMsgState4ProcessedById(pet.getSearch_master_msg_id());
        //获取当前操作人所属店铺。
        Employee employee = employeeMapper.loadByLoginInfoId(currentLoginInfo.getId());
        //宠物设置店铺。
        pet.setShop_id(employee.getShop_id());
        //保存宠物信息，设置自增id
        petMapper.add(pet);
        //设置宠物id并保存宠物的详情。
        PetDetail detail = pet.getDetail();
        detail.setPet_id(pet.getId());
        petDetailMapper.add(detail);
        //生成收购订单和支付单调用支付。
        SearchMasterMsg msg = searchMasterMsgMapper.loadById(pet.getSearch_master_msg_id());
        PetAcquisitionOrder order = pet2order(pet,msg);

    }


    private PetAcquisitionOrder pet2order(Pet pet, SearchMasterMsg msg) {
        PetAcquisitionOrder order = new PetAcquisitionOrder();
        order.setDigest("[摘要]对"+pet.getName()+"收购订单！");
        order.setState(0);//待支付
        order.setPrice(pet.getCostprice());
        order.setAddress(msg.getAddress());
        String orderSn = CodeGenerateUtils.generateOrderSn(msg.getUser_id());
        order.setOrderSn(orderSn);
        order.setPet_id(pet.getId());
        order.setUser_id(msg.getUser_id());
        order.setPaytype(0);
        order.setShop_id(pet.getShop_id());
        return order;
    }
}
